package com.yunji.news.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yunji.news.common.Result;
import com.yunji.news.entity.ScoreLine;
import com.yunji.news.service.ScoreLineService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 分数线信息公共接口
 *
 * @author yunji
 * @since 2024-11-25
 */
@RestController
@RequestMapping("/score-lines")
@RequiredArgsConstructor
public class ScoreLineController {

    private final ScoreLineService scoreLineService;

    /**
     * 分页查询分数线列表
     */
    @GetMapping
    public Result<Page<ScoreLine>> getScoreLinePage(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestParam(required = false) Long schoolId,
            @RequestParam(required = false) Long majorId,
            @RequestParam(required = false) Integer year,
            @RequestParam(required = false) String batch,
            @RequestParam(required = false) String subjectType) {
        
        Page<ScoreLine> scoreLinePage = new Page<>(page, size);
        Page<ScoreLine> result = scoreLineService.getScoreLinePage(
                scoreLinePage, schoolId, majorId, year, batch, subjectType);
        return Result.success(result);
    }

    /**
     * 获取分数线详情
     */
    @GetMapping("/{id}")
    public Result<ScoreLine> getScoreLineDetail(@PathVariable Long id) {
        ScoreLine scoreLine = scoreLineService.getById(id);
        if (scoreLine == null || scoreLine.getStatus() == 0) {
            return Result.error("分数线数据不存在或已禁用");
        }
        return Result.success(scoreLine);
    }

    /**
     * 根据院校ID获取分数线列表
     */
    @GetMapping("/school/{schoolId}")
    public Result<List<ScoreLine>> getScoreLinesBySchoolId(@PathVariable Long schoolId) {
        List<ScoreLine> scoreLines = scoreLineService.getScoreLinesBySchoolId(schoolId);
        return Result.success(scoreLines);
    }

    /**
     * 根据专业ID获取分数线列表
     */
    @GetMapping("/major/{majorId}")
    public Result<List<ScoreLine>> getScoreLinesByMajorId(@PathVariable Long majorId) {
        List<ScoreLine> scoreLines = scoreLineService.getScoreLinesByMajorId(majorId);
        return Result.success(scoreLines);
    }

    /**
     * 获取最近几年的分数线数据
     */
    @GetMapping("/recent")
    public Result<List<ScoreLine>> getRecentScoreLines(
            @RequestParam(defaultValue = "3") Integer years,
            @RequestParam(required = false) Long schoolId,
            @RequestParam(required = false) String batch) {
        
        List<ScoreLine> scoreLines = scoreLineService.getRecentScoreLines(years);
        
        // 可以根据参数进一步过滤
        if (schoolId != null) {
            scoreLines = scoreLines.stream()
                    .filter(sl -> sl.getSchoolId().equals(schoolId))
                    .collect(Collectors.toList());
        }
        
        if (batch != null && !batch.trim().isEmpty()) {
            scoreLines = scoreLines.stream()
                    .filter(sl -> batch.equals(sl.getBatch()))
                    .collect(Collectors.toList());
        }
        
        return Result.success(scoreLines);
    }

    /**
     * 根据年份获取分数线列表
     */
    @GetMapping("/year/{year}")
    public Result<List<ScoreLine>> getScoreLinesByYear(@PathVariable Integer year) {
        List<ScoreLine> scoreLines = scoreLineService.getScoreLinesByYear(year);
        return Result.success(scoreLines);
    }

    /**
     * 获取所有年份
     */
    @GetMapping("/years")
    public Result<List<Integer>> getYears() {
        List<Integer> years = scoreLineService.getYears();
        return Result.success(years);
    }

    /**
     * 获取所有批次
     */
    @GetMapping("/batches")
    public Result<List<String>> getBatches() {
        List<String> batches = scoreLineService.getBatches();
        return Result.success(batches);
    }

    /**
     * 获取所有科目类型
     */
    @GetMapping("/subject-types")
    public Result<List<String>> getSubjectTypes() {
        List<String> subjectTypes = scoreLineService.getSubjectTypes();
        return Result.success(subjectTypes);
    }

    /**
     * 分数线查询分析
     * 根据分数查找可能的院校和专业
     */
    @GetMapping("/analyze")
    public Result<List<ScoreLine>> analyzeByScore(
            @RequestParam Integer score,
            @RequestParam(required = false) String subjectType,
            @RequestParam(required = false) Integer year,
            @RequestParam(defaultValue = "20") Integer limit) {
        
        if (score == null || score < 0 || score > 1000) {
            return Result.error("请输入有效的分数(0-1000)");
        }
        
        // 查询分数线在该分数附近的院校专业
        // 这里简化处理，实际可以根据业务需求进行更复杂的分析
        Page<ScoreLine> scoreLinePage = new Page<>(1, limit);
        Page<ScoreLine> result = scoreLineService.getScoreLinePage(
                scoreLinePage, null, null, year, null, subjectType);
        
        // 过滤出分数线范围内的数据
        List<ScoreLine> filteredLines = result.getRecords().stream()
                .filter(sl -> sl.getMinScore() != null && sl.getMaxScore() != null)
                .filter(sl -> score >= sl.getMinScore() && score <= sl.getMaxScore())
                .collect(Collectors.toList());
        
        return Result.success(filteredLines);
    }
}